package com.sos.core
{
	import com.sos.utils.ClassUtils;
	
	public class UniqueClassType
	{
		
		// -------------------------------------------------------------------------------
		// CONSTANTS
		// -------------------------------------------------------------------------------		
		
		public static const SELF:UniqueClassType			= new UniqueClassType(1, "self", 	"Self");
		public static const SHARED:UniqueClassType			= new UniqueClassType(2, "shared", "Shared");
		
		
		// -------------------------------------------------------------------------------
		// STATIC
		// -------------------------------------------------------------------------------
		
		private static var _initialized:Boolean = false;
		private static var _list:Vector.<UniqueClassType>;
		
		// Fake static constructor
		{
			// Fill the internal list
			_list = new Vector.<UniqueClassType>();
			_list.push(UniqueClassType.SELF);
			_list.push(UniqueClassType.SHARED);
			
			// Init!
			_initialized = true;
		}
		
		// -------------------------------------------------------------------------------
		// FIELDS
		// -------------------------------------------------------------------------------
		
		private var _id:uint;
		private var _name:String;
		private var _sign:String;
		
		// -------------------------------------------------------------------------------
		// CONSTRUCTORS
		// -------------------------------------------------------------------------------
		
		public function UniqueClassType(id:uint, sign:String, name:String)
		{
			// Safe unique instance lock
			if (_initialized) throw new Error("UniqueClassType enum already created!");
			// Init
			_id    = id;
			_name  = name;
			_sign  = sign;
		}

		// -------------------------------------------------------------------------------
		// PROPERTIES
		// -------------------------------------------------------------------------------		
		
		public function get id():uint
		{
			return _id;
		}
		
		public function get sign():String
		{
			return _sign;
		}
		
		public function get name():String
		{
			return _name;
		}
		
		// -------------------------------------------------------------------------------
		// PUBLIC METHODS
		// -------------------------------------------------------------------------------
		
		public function toString():String
		{
			return ClassUtils.getName(this) + "." + _name;
		}
		
		// -------------------------------------------------------------------------------
		// STATIC METHODS
		// -------------------------------------------------------------------------------
		
		
			
		/**
		 * Search a Role by given id 
		 * @param id Id of the card type to find
		 * @return PlayerRole constant for the given id, null if no Role
		 * for the given id has been found
		 */		
		public static function getById(id:uint):UniqueClassType
		{
			// Search the value into the list
			for (var i:int=0; i<_list.length; i++)
			{
				if (_list[i].id == id) return _list[i];
			}
			throw new Error("UniqueClassType for id '" + id + "' unknown!");
		}
		
		/**
		 * Search a Role by given name 
		 * @param name Name of the player role to find
		 * @return PlayerRole constant for the given id, null if no Role
		 * for the given name has been found
		 */		
		public static function getByName(name:String):UniqueClassType
		{
			// Search the value into the list
			for (var i:int=0; i<_list.length; i++)
			{
				if (_list[i].name == name) return _list[i];
			}
			throw new Error("UniqueClassType for name '" + name + "' unknown!");
		}
		
		/**
		 * Search a Role by given sign 
		 * @param name sign of the player role to find
		 * @return PlayerRole constant for the given id, null if no Role
		 * for the given sign has been found
		 */		
		public static function getBySign(sign:String):UniqueClassType
		{
			
			
			// Search the value into the list
			for (var i:int=0; i<_list.length; i++)
			{
				if (_list[i].sign == sign) return _list[i];
			}
			throw new Error("UniqueClassType for sign '" + sign + "' unknown!");
		}

		
		
	}
}